// import router from '@/router'
import {
  reqHotGoods, reqAreaSell, reqMarketEXS,
  reqStatistics, reqMarket1, reqMarket2, reqMarketPie,
  reqNumberOfpoints, CooperativeMerchants
} from '@/api/good'
// import moment from 'moment'
export default {
  namespaced: true,
  state: {
    /**
     * 商品热搜
     */
    hotGoods: [],
    // exs
    series: [],
    xAxis: [],
    seriesLeft: [],
    xAxisLeft: [],
    id: '',
    // exs
    // 工单统计
    StatisticsSta: {},
    // 销售统计
    StatisticsSta2: {},
    // 合作商点位数Top5
    MarketPieSTATE: [],
    /**
     * 点为数 &&合作商家
     */
    numberOfpointsCooperativeMerchants: {}
  },
  mutations: {
    hotGoodsMutations (state, val) {
      state.hotGoods = val
      // console.log(val)
    },
    reqAreaSellMu (state, val) {
      state.series = val.series
      state.xAxis = val.xAxis
      // state.xAxis.splice(0, , )
    },
    reqMarketEXSMu (state, val) {
      state.seriesLeft = val.series
      state.xAxisLeft = val.xAxis
    },
    i (state, val) {
      state.id = val
    },
    // 工单统计  // 销售统计
    StatisticsMu (state, val) {
      state.StatisticsSta = val[0]
      state.StatisticsSta2 = val[1]
      // console.log(val[0])
    },
    // 合作商点位数Top5
    MarketPieMu (state, val) {
      state.MarketPieSTATE = val
    },
    // 合作商家 // 点为数
    CooperativeMerchantsMu (state, val) {
      state.numberOfpointsCooperativeMerchants = val
    }
  },
  actions: {
    // reqAreaSell
    async topSearch (context) {
      // console.log(1)
      // 商品热搜
      const res = await reqHotGoods()
      context.commit('hotGoodsMutations', res)
      // 工单统计
      const res1 = await reqStatistics()
      // 用于合并对象
      function fn (arr) {
        const obc = {}
        for (let i = 0; i < arr.length; i++) {
          // 拿出k
          const item = Object.keys(arr[0])
          for (let j = 0; j < item.length; j++) {
            const id = i + 1
            // 判断 如果 arr[id] 大于 length 那个
            if (id >= arr.length) {
              return obc
            }
            // 对象有去重的特性
            obc[item[j]] = arr[i][item[j]] + arr[id][item[j]]
          }
        }
      }
      // fn(res1)
      // console.log(fn(res1))

      // 销售统计
      const res2 = await reqMarket1()
      const res3 = await reqMarket2()
      const MarketObj = {
        quantity: res2,
        one: res3
      }
      // console.log(MarketObj)
      const arr = []
      arr[0] = MarketObj
      arr[1] = fn(res1)
      // console.log(arr)
      context.commit('StatisticsMu', arr)
      // console.log(res1, 2, res2, 3, res3)
    },
    // exs
    async topSearchE (context, ke) {
      // 根据地区汇总销售额数据(销售量分布)exs
      context.commit('i', ke)
      const val = await reqAreaSell(ke)
      context.commit('reqAreaSellMu', val)
      // 左边
      const res = await reqMarketEXS(ke)
      context.commit('reqMarketEXSMu', res)

      // 合作商点位数Top5
      const res2 = await reqMarketPie()
      // console.log(res2)
      context.commit('MarketPieMu', res2)
      // 点为数
      const res3 = await reqNumberOfpoints()
      // 合作商家
      const res4 = await CooperativeMerchants()
      console.log(res3, res4)
      context.commit('CooperativeMerchantsMu', { one: res3, one2: res4 })
    }

  }

}
